package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.resources.RedisMessageConstant;
import com.itheima.resources.SMSUtils;
import com.itheima.resources.ValidateCodeUtils;
import com.itheima.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RequestMapping(value="/member",name = "用户模块")
@RestController
public class MemberController {
    @Reference
    private MemberService memberService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value = "/SendLoginVerifyCode.do",name = "发送登陆的验证码短信")
    public Result SendLoginVerifyCode(@RequestParam("telephone") String telephone ){
        try {
            //产生4位数的验证码
            String code = ValidateCodeUtils.generateValidateCode(4);

            //将手机号作为key,验证码作为value存储到redis中,方便后台验证
            Jedis jedis = jedisPool.getResource();
            //存储到redis中,并设置存储时间 90秒.超过时间即失效
            jedis.setex(telephone+"_"+ RedisMessageConstant.SENDTYPE_LOGIN,90,code);
            //释放jedis
            jedis.close();

            //发送短信
            SMSUtils.sendCode(telephone,code);

            return new Result(true,"短信验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "短信验证码发送失败");
        }
    }
    @RequestMapping(value = "/memberLogin.do",name = "登陆功能")
    public Result memberLogin(@RequestBody Map map){
        try {
            Jedis jedis = jedisPool.getResource();
            //查询redis中存储的验证码
            String redisCode = jedis.get(map.get("telephone") + "_" + RedisMessageConstant.SENDTYPE_LOGIN);
            //释放jedis
            jedis.close();

            //获取用户提交的验证码
            String code = (String) map.get("validateCode");

            if (redisCode==null){
                return new Result(false,"验证码已过时,请重新获取");
            }

            //比较验证码是否正确
            if (!redisCode.equals(code)){
                return new Result(false,"登陆失败,验证码错误");
            }
            //判断新老用户
            Member member=memberService.findMemberByTelephone((String)map.get("telephone"));
            //如果是新客户,就添加到客户表中去
            if (member==null){
                member = new Member();
                //电话号
                String telephone = (String) map.get("telephone");
                //系统时间--注册时间
                Date rtime = new Date(System.currentTimeMillis());
                member.setPhoneNumber(telephone);
                member.setRegTime(rtime);
                //调用生产者的添加用户方法
                memberService.addMember(member);
            }


            return new Result(true,"登陆成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"登陆失败");
        }
    }


}
